﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Clustering
{
    public partial class KMeansClusteringConfigUC : UserControl
    {
        private KMeansClusteringVideoSeparator kMeansVideoSeparator;

        public KMeansClusteringVideoSeparator KMeansVideoSeparator
        {
            get { return kMeansVideoSeparator; }
            set
            {
                if (kMeansVideoSeparator != null)
                {
                    kMeansVideoSeparator.ForegroundSet -= new EventHandler(kMeansVideoSeparator_ForegroundSet);
                    kMeansVideoSeparator.BackgroundSet -= new EventHandler(kMeansVideoSeparator_BackgroundSet);
                }
                kMeansVideoSeparator = value;
                if (kMeansVideoSeparator != null)
                {
                    kMeansVideoSeparator.ForegroundSet += new EventHandler(kMeansVideoSeparator_ForegroundSet);
                    kMeansVideoSeparator.BackgroundSet += new EventHandler(kMeansVideoSeparator_BackgroundSet);
                }
            }
        }



        public KMeansClusteringConfigUC()
        {
            InitializeComponent();
        }

        private void pbBackground_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Bitmap bmp = (Bitmap)Bitmap.FromFile(openFileDialog.FileName);
                    pbBackground.Image = bmp;
                    kMeansVideoSeparator.SetBackground(bmp);
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void pbForeground_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Bitmap bmp = (Bitmap)Bitmap.FromFile(openFileDialog.FileName);
                    pbForeground.Image = bmp;
                    kMeansVideoSeparator.SetForeground(bmp);
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            try
            {
                if (kMeansVideoSeparator != null)
                {
                    bwInit.RunWorkerAsync();
                }
            }
            catch
            {
            }
        }

        private void btnSwap_Click(object sender, EventArgs e)
        {
            if (kMeansVideoSeparator != null)
            {
                kMeansVideoSeparator.SwapRowers();
            }
        }

        private void bwInit_DoWork(object sender, DoWorkEventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //kMeansVideoSeparator.Init(pbBackground.Image as Bitmap, pbForeground.Image as Bitmap);
            kMeansVideoSeparator.Init();
            sw.Stop();
            e.Result = "Init done in " + sw.ElapsedMilliseconds + " ms";
        }

        private void bwInit_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lblMessage.Text = e.Result as string;
        }

        private void pbBackground_DoubleClick(object sender, EventArgs e)
        {
            kMeansVideoSeparator.SetBackgroundNow();
        }

        private void pbForeground_DoubleClick(object sender, EventArgs e)
        {
            kMeansVideoSeparator.SetForegroundNow();
        }

        void kMeansVideoSeparator_BackgroundSet(object sender, EventArgs e)
        {
            //TODO
        }

        void kMeansVideoSeparator_ForegroundSet(object sender, EventArgs e)
        {
            //TODO
        }

    }
}
